﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ESHOP.Domain;
using ESHOP.DAO;
using ESHOP.UTIL;
using E_Shop.Common;
namespace E_Shop
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        
        private readonly DanhMucDAO _danhmuc = new DanhMucDAO();
        private readonly QuocGiaDAO _countrydao = new QuocGiaDAO();
        private readonly DanhMucDAO _categorydao = new DanhMucDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                BindDataforRepeater();
                LoadReferences();
            }
           
        }

        
       
        public void BindDataforRepeater()
        {
            var lst = _danhmuc.GetList();
            repeatmenu.DataSource = lst;
            repeatmenu.DataBind();

        }
        public void LoadReferences()
        {
            CommonFunc.BindDropDownList(ddlCaegory, _categorydao.GetList(), "TENDM", "ID", true);
            CommonFunc.BindDropDownList(ddlCountry,_countrydao.GetList(), "TENQUOCGIA", "ID", true);
        }
        protected void btnsearch_Click(object sender, EventArgs e)
        {
             String textsearch = txtSearch.Text.Trim();
             if (textsearch.Equals("All") || textsearch.Equals("Selling Leads") || textsearch.Equals("TradeMall") || textsearch.Equals("Products") || textsearch.Equals("Companies") || textsearch.Equals("Buyers"))
                 textsearch = "";
            String idcategory = ddlCaegory.SelectedValue;
            String idcounttry = ddlCountry.SelectedValue;
            int  a = int.Parse(Hiddenfeil.Value.Trim().ToString());
            var keysearch = "default.aspx";
            if (a == 0)
            {
                keysearch = String.Format(WebUrl.AllSEARCH, true);
            }
            else if (a == 1)
            {
                keysearch = String.Format(WebUrl.TRADELEAD_SELLING + "&id={0}&textsearch={1}&idcategory={2}&idcounttry={3}",
                "", textsearch, idcategory, idcounttry);
            }
            else if (a == 2)
            {
            }else if(a==3){
                keysearch = String.Format(WebUrl.COMPANYTRADELIST + "&id={0}&textsearch={1}&idcategory={2}&idcounttry={3}",
                "", textsearch, idcategory, idcounttry);
            }
            else if (a == 4)
            {
            }
            else if (a == 5)
            {
                idcategory = ddlCaegory.SelectedItem.Text.Trim().ToString();
                keysearch = String.Format(WebUrl.COMPANYTRADEBUYLIST + "&id={0}&textsearch={1}&idcategory={2}&idcounttry={3}",
                "", textsearch, idcategory, idcounttry);
            }
            
            Response.Redirect(keysearch, true);
        }
        public void RegisterStartupScript(string key, string script)
        {
            ScriptManager.RegisterStartupScript(phrJsRunner, phrJsRunner.GetType(), key, script, true);
        }
        public void UnblockDialog_sitermaster(string divId)
        {
            RegisterStartupScript(string.Format("jsUnblockDialog-{0}-{1}", divId, Guid.NewGuid()),
                string.Format("unblockDialog('{0}');", divId));
        }
        public void HideDialog_sitermaster(string divId)
        {
            RegisterStartupScript(string.Format("jsCloseDialog-{0}-{1}", divId, Guid.NewGuid()),
                string.Format("closeDialog('{0}');", divId));
        }

        public void FocusAndSelect_sitermaster(string id)
        {
            RegisterStartupScript(string.Format("jsFocusAndSelect-{0}-{1}", id, Guid.NewGuid()),
                string.Format("FocusAndSelect('{0}');", id));
        }

        public void SetControlValue_sitermaster(string id, string value)
        {
            RegisterStartupScript(string.Format("jsSetControlValue-{0}-{1}", id, Guid.NewGuid()),
                string.Format("setControlValue('{0}', '{1}');", id, value));
        }

        public void SetLabel_sitermaster(string id, string value)
        {
            RegisterStartupScript(string.Format("jsSetLabel-{0}-{1}", id, Guid.NewGuid()),
                string.Format("setLabelText('{0}', '{1}');", id, value));
        }

        public void SetReadonly_sitermaster(string id, bool isReadonly)
        {
            if (isReadonly)
                RegisterStartupScript(string.Format("jsSetReadonly-{0}-{1}", id, Guid.NewGuid()),
                    string.Format("setReadonly('{0}');", id));
            else
                RegisterStartupScript(string.Format("jsRemoveReadonly-{0}-{1}", id, Guid.NewGuid()),
                    string.Format("removeReadonly('{0}');", id));
        }

        public void ShowError_sitermaster(string message, string controlId)
        {
            RegisterStartupScript(string.Format("jsShowErrorWithFocus-{0}-{1}", controlId, Guid.NewGuid()),
                string.Format("showErrorWithFocus('{0}', '{1}');", ConvertUtil.CleanTextHTML2(message), controlId));
        }

        public void ShowError_sitermaster(string message)
        {
            RegisterStartupScript(string.Format("jsShowError-{0}", Guid.NewGuid()),
                string.Format("showError('{0}');", ConvertUtil.CleanTextHTML2(message)));
        }

        public void ShowInfor_sitermaster(string message)
        {
            RegisterStartupScript(string.Format("jsShowInfor-{0}", Guid.NewGuid()),
                string.Format("showInfor('{0}');", ConvertUtil.CleanTextHTML2(message)));
        }

        public void ShowWarning_sitermaster(string message)
        {
            RegisterStartupScript(string.Format("jsShowWarning-{0}", Guid.NewGuid()),
                string.Format("showWarning('{0}');", ConvertUtil.CleanTextHTML2(message)));
        }


        public void OpenWaitingDialog_sitermaster()
        {
            RegisterStartupScript(string.Format("jsOpenWaitingDialog-{0}", Guid.NewGuid()),
                "openWaitingDialog();");
        }

        public void UnblockWaitingDialog_sitermaster()
        {
            RegisterStartupScript(string.Format("jsUnblockWaitingDialog-{0}", Guid.NewGuid()),
                "unblockWaitingDialog();");
        }

        public void CloseWaitingDialog_sitermaster()
        {
            RegisterStartupScript(string.Format("jsCloseWaitingDialog-{0}", Guid.NewGuid()),
                "closeWaitingDialog();");
        }
        
    }
}
